<script type="text/javascript"><!--//--><![CDATA[//><!--
var LOVPerangkat = function(ao_option){
    try{
        if(ao_option==undefined){ ao_option={}; }
        var lf_onSelect = (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
        
        // Create Popup
        var $popup  = openPopup({ title:'Daftar Produk' , width:700, height:370 });
        var ls_id   = $popup.attr('id');
        
        // Append Datagrid Container
        var ls_gridName = 'grid_'+ls_id;
        $popup  .html(  $('<div>')  .attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
                                    .html(  $('<div>').html($('<form>') .attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
                                                                        .html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
                                                            )
                                            )
                        )
                .append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
        
        $('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
        $('#btn_search'+ls_id).linkbutton({  
            plain:true  
        }); 
        
        // Build Datagrid
        var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
        $('#'+ls_gridName).datagrid({  
            url:gs_path+'/mis_data_perangkat1/getdata2/'+ls_param,
            queryParams: $('#fm_search_'+ls_id).serializeObject(),
            height:337,
            pagination:true,
            singleSelect:true,
            columns:[[
                { field : 'id_perangkat', title : 'id_perangkat', width : 170, sortable:true },
                { field : 'kode_barang', title : 'Kode Barang', width : 120, sortable:true },
                { field : 'nama_barang', title : 'Nama Barang', width : 170, sortable:true },
                { field : 'nama_merek', title : 'Nama Merek', width : 170, sortable:true },
                { field : 'jumlah', title : 'Stock Barang', width : 100, sortable:true },
                { field : 'kondisi_barang', title : 'Kondisi Barang', width : 170, sortable:true },
            ]],
            onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
        });
        
    } catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};

    var grid_reload = function(){
        $('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
    };

    $(document).ready(function() {

        // Setup Datagrid
        $('#datagrid').datagrid({  
            url:gs_path+'/'+controller+'/getdata', 
            queryParams: $('#fm_search').serializeObject(),
            height:350,
            pagination:true,
            columns:[[
  			    { field : 'ck', checkbox:true, width: 40, align: 'center' },
                { field : 'kode_pengadaan', title : 'Kode Pengadaan', width : 100, sortable:true },
                { field : 'nama_pengaju', title : 'Nama Pengaju', width : 200, sortable:true },
                { field : 'nama_departemen', title : 'Departemen', width : 250, sortable:true },
				{ field : 'tanggal_pengajuan', title : 'Tanggal Pengajuan', width : 180, sortable:true },
                { field : 'nama_barang', title : 'Nama Barang', width : 200, sortable:true },
				{ field : 'jenis_kebutuhan', title : 'Jenis Kebutuhan', width : 150, sortable:true },
                { field : 'jumlah_barang', title : 'Jumlah Barang', width : 150, sortable:true },
				{ field : 'uraian_pengajuan', title : 'Uraian Pengajuan', width : 250, sortable:true },
				{ field : 'tanggal_estimasi', title : 'Tanggal Estamasi', width : 250, sortable:true }
            ]]
        });
        
        
        // Setup Form
        $('#form_master').setupForm([
            { name: 'kode_pengadaan', type: 'text', size: 'medium', maxlength: 10, noempty:true },
            { name: 'nama_pengaju', type: 'text', size: 'medium', maxlength: 100, noempty:true },
            { name: 'nama_unit', type:'lov', data: <?php echo json_encode($unitkerja); ?>, size: 'long', noempty:true },
            { name: 'tanggal_pengajuan', type: 'date', noempty:true },
            { name: 'kode_barang', type:'lov', size: 'long',
                onClearButton:function(){
                    $(this).setval(''); 
                    $('#nama_unit').setval('');
                    $('#nama_merek').setval(''); 
                    $('#stock').setval('');
                    $('#kondisi_barang').setval('');
                    $('#id_perangkat').setval('');
                },
                onActionButton:function(){ 
                    LOVPerangkat({ onSelect:function(row){ 
                       $('#kode_barang').setval(row.kode_barang);
                       $('#nama_barang').setval(row.nama_barang);
                       $('#nama_merek').setval(row.nama_merek); 
                       $('#stock').setval(row.jumlah);
                       $('#kondisi_barang').setval(row.kondisi_barang);
                       $('#id_perangkat').setval(row.id_perangkat);
                    }}); 
                }
            },
            { name: 'nama_merek', type: 'text', size: 'long', maxlength: 100, noempty:true },
            { name: 'nama_barang', type: 'text', size: 'long', maxlength: 100, noempty:true },
            { name: 'stock', type: 'text', size: 'long', maxlength: 100, noempty:true },
            { name: 'kondisi_barang', type: 'text', size: 'long', maxlength: 100, noempty:true },
            { name: 'jumlah_barang', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'jenis_kebutuhan', type: 'text', size: 'long', maxlength: 100 },
			{ name: 'uraian_pengajuan',type:'textarea', size: 'long', maxlength: 250 },
			{ name: 'tanggal_estimasi', type: 'date', noempty:true  }
        ]);
        
        
        // Create Dialog
        $('#dialog_container').dialog({
            closed:true,
            modal:true,
            title: '_',
            width:600,
			height:550,
            buttons:[{
                text:'Save',
                iconCls:'icon-save',
                handler:function(){
                    $('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); loadPage(gs_path + '/' + controller + '/index') }} );
                    //loadPage(gs_path + '/' + controller + '/index');
                }
            },{
                text:'Close',
                iconCls:'icon-cancel',
                handler:function(){
                    $('#dialog_container').dialog('close');
                }
            }]
        });
        
        // Set Action Button
        var buttons = [ { label: 'Add', icon: 'add', onclick: function() {

                            $.ajax({
                                url:gs_path+'/'+controller+'/id_auto',
                                type:'POST',
                                dataType:'JSON',
                                }).done(function(data){
                                    $('#id_pengadaan').setval(data['id_pengadaan_auto']);
                                    $('#kode_pengadaan').setval(data['kode_pengadaan_auto']);
                                });

                            resetError();
                            $('#dialog_container').dialog('open');
                            $('#form_master').data('mode', 'create');
                            $('#form_master').parent().parent().parent().prev().find('.panel-title').html('Tambah Data Permintaan');
                            $('#kode_pengadaan,#kondisi_barang,#stock,#nama_barang,#nama_merek').readonly();
                            $('#kode_pengadaan, #nama_pengaju, #nama_unit, #tanggal_pengajuan, #kode_barang, #jenis_kebutuhan,#uraian_pengajuan,#tanggal_estimasi').val('');
                            $('#kode_barang').focus();
                        
                            //Calculation Keyup Function                        
                            $('#jumlah_barang').keyup(function() {
                                var jumlah =  parseInt($('#jumlah_barang').val());
                                var stock = parseInt($('#stock').val());
                                var j = stock + 1;
                                //alert(jum_awal);
                                if(jumlah>=j){
                                    alert(ls_eror ='input jumlah melebihi stock !\n');
                                    $('#jumlah_barang').setval('0');
                                    $('#jum_akhir').setval('');
                                } else {
                                    var jum_akhir = stock - jumlah;
                                    $('#update_stock').setval(jum_akhir);
                                }
                            });

                        } },
                        { label: 'Edit', icon: 'edit', onclick: function() {
                            var oSel=$('#datagrid').datagrid('getSelected'); 
                            if(oSel==null){ 
                                alert('Please select row!'); 
                            } else{
                                //alert(oSel['kode_barang']);
                                resetError();
                                $('#dialog_container').dialog('open'); 
                                $('#form_master').data('mode', 'edit');
                                $('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Data Permintaan');
                                $('#kode_pengadaan').readonly();
                                jsonToForm(oSel); 
                                $('#kode_pengadaan').focus();
                                $('#kode_pengadaan,#kondisi_barang,#stock,#kode_barang,#nama_barang,#nama_merek').readonly();
                                $('#jum_awal').setval(oSel['jumlah']);
                                $('#stock').setval(oSel['jumlah']);
                                $('#jumlah_pinjam').setval(oSel['jumlah_barang']);
                                $('#kondisi_barang').setval(oSel['kondisi_barang']);
                                $('#id_perangkat').setval(oSel['id_perangkat']);
                                $('#id_pengadaan').setval(oSel['id_pengadaan']);

                                    //Calculation Keyup Function
                                    $('#jumlah_barang').keyup(function() {
                                        var jum_awal =  parseInt(oSel['jumlah']);
                                        var jumlah =  parseInt($('#jumlah_barang').val());
                                        var stock = parseInt($('#stock').val());
                                        var jumlah_pinjam = oSel['jumlah_barang'];
                                        var ls_eror = '';

                                            if(jumlah_pinjam<=jumlah){
                                                $('#jum_awal').setval(jum_awal);
                                                var jum_akhir = jumlah_pinjam - jumlah;
                                                $('#jum_akhir').setval(jum_akhir);
                                                var jum_cal = jum_akhir *= (-1) ;
                                                //alert(jum_cal);
                                                var j = stock + 1;
                                                $('#jum_akhir').setval(jum_cal);
                                                    if(jum_akhir>=j){
                                                        alert(ls_eror ='input jumlah melebihi stock !\n');
                                                        $('#jumlah_barang').setval(oSel['jumlah_barang']);
                                                        $('#jum_akhir').setval('');
                                                    } else if($('#jumlah_barang').val()==0){
                                                        var update_stock = jumlah_pinjam + stock;
                                                        $('#update_stock').setval(update_stock);
                                                    } else {
                                                        var update_stock = stock - jum_cal;
                                                        $('#update_stock').setval(update_stock);
                                                    }
                                             } if(jumlah_pinjam>=jumlah) {
                                                //alert(jum_akhir);
                                                $('#jum_awal').setval(jum_awal);
                                                var jum_akhir = jumlah_pinjam - jumlah;
                                                //alert(jum_akhir);
                                                $('#jum_akhir').setval(jum_akhir);
                                                var update_stock = stock + jum_akhir;
                                                $('#update_stock').setval(update_stock);
                                            }
                                    });
                            } 
                        } },
                        {label: 'Delete', icon: 'delete', onclick: function() { 
                            var oSel = $('#datagrid').datagrid('getSelected'); 
                            if (oSel == null) { 
                                $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
                            } else { 
                                deleteData(['id_pengadaan']);
                            } 
                        }}];
        setAction(buttons);
        
        
        $('#fm_search').focusFirst();
    });
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
            <table class="tb_form" style="margin-bottom:0px">
            <tr>
                <td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                    <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>


<div id="dialog_container" style="padding:5px;width:600px;height:250px;">
    <form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <input id="id_pengadaan" name="id_pengadaan" type="hidden" />
        </td>
    <tr>
        <td class="label" style="width:150px">Kode Pengadaan</td>
        <td><input id="kode_pengadaan" /></td>
    </tr>
    <tr>
        <td class="label" style="width:150px">Nama Pengaju</td>
        <td><input id="nama_pengaju" /></td>
    </tr>
    <tr>
        <td class="label">Department</td>
        <td><input id="nama_unit" /></td>
    </tr>
    <tr>
        <td class="label">Tanggal Pengajuan</td>
        <td><input id="tanggal_pengajuan" /></td>
    </tr>
    <tr>
        <td class="label">Kode Barang</td>
        <td><input id="kode_barang" /></td>
    </tr>
    <tr>
        <td class="label">Nama Barang</td>
        <td><input id="nama_barang" /></td>
    </tr>
    <tr>
        <td class="label">Nama Merek</td>
        <td><input id="nama_merek" /></td>
    </tr>
    <tr>
        <td class="label">Stock Barang</td>
        <td><input id="stock" /><input id="jum_awal" name="jum_awal" type="hidden" /><input id="id_perangkat" name="id_perangkat" type="hidden" /></td>
    </tr>
    <tr>
        <td class="label">Kondisi Barang</td>
        <td><input id="kondisi_barang" /></td>
    </tr>
    <tr>
        <td class="label">Jumlah Permintaan</td>
        <td><input id="jumlah_barang" /><input id="jumlah_pinjam" name="jumlah_pinjam" type="hidden" /><input id="jum_akhir" name="jum_akhir" type="hidden" /><input id="update_stock" name="update_stock" type="hidden" /></td>
    </tr>
    <tr>
        <td class="label">Jenis Kebutuhan</td>
        <td><input id="jenis_kebutuhan" /></td>
    </tr>
    <tr>
        <td class="label">Uraian Pengajuan</td>
        <td><input id="uraian_pengajuan" /></td>
    </tr>
    <tr>
        <td class="label">Estimasi Ketersediaan Permintaan</td>
        <td><input id="tanggal_estimasi" /></td>
    </tr>
    </table>
    </form>
</div>